package servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.BorrowDao;
import dao.UserDao;
import dao.impl.BorrowDaoImpl;
import dao.impl.UserDaoImpl;
import model.User;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserDao ud;
	private BorrowDao bwd;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserServlet() {
		super();
		// TODO Auto-generated constructor stub
		ud = new UserDaoImpl();
		bwd = new BorrowDaoImpl();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String action = (String) request.getParameter("action");
		try {
			if (action.equals("login")) {
				login(request, response);
			} else if (action.equals("register")) {
				register(request, response);
			} else if (action.equals("updateInfo")) {
				updateInfo(request, response);
			} else if (action.equals("getalluser")) {
				getalluser(request, response);
			} else if (action.equals("deleteUser")) {
				deleteUser(request, response);
			} else if (action.equals("logout")) {
				request.getSession().invalidate();
				response.sendRedirect("IndexServlet");
			}
		} catch (SQLException e) {

		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	private void register(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
		String id = request.getParameter("UserId");
		String pwd = request.getParameter("UserPwd");
		User user = new User();
		user.setUserId(id);
		user.setUserPwd(pwd);
		UserDao ud = new UserDaoImpl();
		if (ud.register(user)) {
			response.sendRedirect("pages/logon.jsp");
		} else {
			response.sendRedirect("pages/register.jsp");
		}

	}

	private void login(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
		String id = request.getParameter("UserID");
		String pwd = request.getParameter("UserPwd");
		String userGrade = request.getParameter("userGrade");
		User user = ud.getUserByIdAndPwd(id, pwd, userGrade);
		if (user != null) {
			HttpSession session = request.getSession();
			session.setAttribute("logoner", user);
			response.sendRedirect("IndexServlet");
		} else {
			response.sendRedirect("pages/logon.jsp");
		}
	}

	private void updateInfo(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
		String id = request.getParameter("UserID");
		String userGrade = request.getParameter("userGrade");
		String name = request.getParameter("UserName");
		String pwd = request.getParameter("UserPwd");
		String phone = request.getParameter("UserPhone");
		String address = request.getParameter("UserAddress");
		User userNew = new User();
		userNew.setUserId(id);
		userNew.setUserGrade(Integer.parseInt(userGrade));
		userNew.setUserName(name);
		userNew.setUserPwd(pwd);
		userNew.setUserAddress(address);
		userNew.setUserPhone(phone);
		UserDao ud = new UserDaoImpl();
		if (ud.updateUser(userNew)) {
			User user = ud.getUserByIdAndPwd(id, pwd, userGrade);
			HttpSession session = request.getSession();
			User user1 = (User) session.getAttribute("logoner");
			if (user1.getUserGrade() == 0) {
				session.setAttribute("userlist", ud.getAllUser());
				response.sendRedirect("pages/userlist.jsp");
			} else if (user1.getUserGrade() == 1) {
				session.setAttribute("logoner", user1);
				response.sendRedirect("IndexServlet");
			}
		} else {
			response.sendRedirect("pages/updateInfo.jsp");
		}

	}

	private void getalluser(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
		HttpSession session = request.getSession();
		User logoner = (User) session.getAttribute("logoner");
		if (logoner.getUserGrade() == 0) {
			session.setAttribute("userlist", ud.getAllUser());
			response.sendRedirect("pages/userlist.jsp");
		} else {
			response.sendRedirect("index.jsp");
		}
	}

	private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
		String userId = request.getParameter("UserID");
		User user = new User();
		user.setUserId(userId);
		ud.deleteUser(user);
		HttpSession session = request.getSession();
		session.setAttribute("userlist", ud.getAllUser());
		response.sendRedirect("pages/userlist.jsp");
	}
}
